package com.idanchuang.component.just.web;

import com.idanchuang.component.base.actuator.DcInfoEndpoint;
import com.idanchuang.component.just.web.base.BaseController;
import com.idanchuang.component.just.web.config.ComWebMvcConfig;
import com.idanchuang.component.just.web.config.ComponentConfig;
import com.idanchuang.component.just.web.config.XssFilterConfig;
import com.idanchuang.component.just.web.filter.WebAccessFilter;
import com.idanchuang.component.just.web.handler.GlobalExceptionHandler;
import com.idanchuang.component.just.web.warmup.ComponentJustWebWarmUp;
import com.idanchuang.sun.client.common.tag.TagCollector;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Collections;

/**
 * @author yjy
 * @date 2019/10/12 16:57
 **/
@Configuration
@Import({ComWebMvcConfig.class})
@ServletComponentScan("com.idanchuang.component.just.web.filter")
public class ComponentWebAutoConfiguration {

    @Bean("justWebComponentConfig")
    @ConfigurationProperties(prefix = "idanchuang.component.web")
    public ComponentConfig componentConfig() {
        return new ComponentConfig();
    }

    @Bean("justWebComponentXssFilterConfig")
    @ConfigurationProperties(prefix = "idanchuang.component.web.filter.xss")
    public XssFilterConfig xssFilterConfig() {
        return new XssFilterConfig();
    }

    @Bean
    @ConditionalOnMissingBean(CorsFilter.class)
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }

    @Bean("justWebComponentMvcConfig")
    public ComWebMvcConfig comWebMvcConfig() {
        return new ComWebMvcConfig();
    }

    @Bean
    @ConditionalOnMissingBean(name = "webAccessFilter")
    public WebAccessFilter webAccessFilter(ComponentConfig componentConfig) {
        return new WebAccessFilter(componentConfig);
    }

    @Bean
    @ConditionalOnMissingBean(name = "globalExceptionHandler")
    public GlobalExceptionHandler globalExceptionHandler(ComponentConfig componentConfig) {
        return new GlobalExceptionHandler(componentConfig);
    }

    @Bean("justWebBaseController")
    @ConditionalOnMissingBean(BaseController.class)
    public BaseController baseController() {
        return new BaseController();
    }

    @Bean
    @ConditionalOnMissingBean(name = "dcComponentBaseTagCollector")
    public TagCollector dcComponentBaseTagCollector() {
        return () -> Collections.singleton(DcInfoEndpoint.getComponentVersion());
    }

    @Bean("acgComponentJustWebWarmUp")
    @ConditionalOnMissingBean(ComponentJustWebWarmUp.class)
    public ComponentJustWebWarmUp componentJustWebWarmUp() {
        return new ComponentJustWebWarmUp();
    }

}
